import { unitWidth, barHeight, barMargin } from '../config';
import { getRandomColor } from '../utils';
import * as zrender from "zrender";

/**
 * 设置鼠标移动交互处理
 * @param {Object} zr ZRender实例
 * @param {Array} tasks 任务数组
 * @param {Object} chartState 图表状态对象
 * @param {Function} redrawChart 重绘图表函数
 * @param {Object} currentGroup 当前操作组
 */
export function setupMouseMoveHandler(zr: any, tasks: Task[], chartState: ChartState, redrawChart: RedrawChartFunction, currentGroup: any) {
  let lastPos: string | undefined;
  let lastDayRect: zrender.Group | undefined;

  function handleMove(e: any) {
    if (currentGroup?.resizing || currentGroup?.dragging) {
      return;
    }

    const x = e.event.zrX - chartState.chartStartX;
    const y = e.event.zrY - chartState.chartStartY;
    const posX = Math.floor(x / unitWidth);
    const posY = Math.floor(y / (barHeight + barMargin));

    if (lastPos !== [posX, posY].join()) {
      lastPos = [posX, posY].join();

      if (posY >= 0 && posY < tasks.length) {
        const hasTask = posY !== tasks.length - 1; // 只能在最后一行插入新任务

        if (hasTask) {
          lastDayRect && zr.remove(lastDayRect);
          return;
        }

        // 创建悬停组
        const dayHoverGroup = createHoverGroup(zr, posX, posY, chartState, tasks, redrawChart);

        lastDayRect && zr.remove(lastDayRect);
        lastDayRect = dayHoverGroup;
        zr.add(dayHoverGroup);
      } else {
        lastDayRect && zr.remove(lastDayRect);
      }
    }
  }

  return handleMove;
}

/**
 * 创建悬停效果组
 * @param {Object} zr ZRender实例
 * @param {number} posX X位置
 * @param {number} posY Y位置
 * @param {Object} chartState 图表状态对象
 * @param {Array} tasks 任务数组
 * @param {Function} redrawChart 重绘图表函数
 * @returns {Object} 悬停组
 */
function createHoverGroup(zr: any, posX: number, posY: number, chartState: ChartState, tasks: Task[], redrawChart: RedrawChartFunction): zrender.Group {
  const dayHoverGroup = new zrender.Group();

  // 点击事件处理
  dayHoverGroup.on('click', function () {
    const taskName = '亚视啦';
    const resourceName = '张三';

    tasks.splice(posY, 0, {
      name: taskName,
      start: posX,
      duration: 1,
      resource: resourceName,
      fillColor: getRandomColor()
    });

    redrawChart(true);
  });

  // 绘制悬停矩形
  const dayRect = new zrender.Rect({
    shape: {
      x: chartState.chartStartX + unitWidth * posX + 5,
      y: chartState.chartStartY + (barHeight + barMargin) * posY,
      width: unitWidth - 10,
      height: barHeight + barMargin,
    },
    style: {
      fill: 'transparent',
      stroke: 'gray',
      lineDash: [5, 7]
    },
    z: 100
  });

  // 绘制行悬停矩形
  const rowHoverRect = new zrender.Rect({
    shape: {
      x: chartState.chartStartX + chartState.lastScrollX,
      y: chartState.chartStartY + (barHeight + barMargin) * posY,
      width: unitWidth * chartState.timeScaleWidth,
      height: barHeight + barMargin,
    },
    style: {
      fill: 'rgba(221, 221, 221, 0.3)',
    },
    z: 1
  });

  // 绘制水平线
  const lineH = new zrender.Line({
    shape: {
      x1: chartState.chartStartX + unitWidth * posX + unitWidth / 2 - 5,
      y1: chartState.chartStartY + (barHeight + barMargin) * posY + (barHeight + barMargin) / 2,
      x2: chartState.chartStartX + unitWidth * posX + unitWidth / 2 + 5,
      y2: chartState.chartStartY + (barHeight + barMargin) * posY + (barHeight + barMargin) / 2
    },
    style: {
      stroke: 'gray'
    },
    z: 100
  });

  // 绘制垂直线
  const lineV = new zrender.Line({
    shape: {
      x1: chartState.chartStartX + unitWidth * posX + unitWidth / 2,
      y1: chartState.chartStartY + (barHeight + barMargin) * posY + (barHeight + barMargin) / 2 - 5,
      x2: chartState.chartStartX + unitWidth * posX + unitWidth / 2,
      y2: chartState.chartStartY + (barHeight + barMargin) * posY + (barHeight + barMargin) / 2 + 5
    },
    style: {
      stroke: 'gray'
    },
    z: 100
  });

  dayHoverGroup.add(dayRect);
  dayHoverGroup.add(lineH);
  dayHoverGroup.add(lineV);
  dayHoverGroup.add(rowHoverRect);

  return dayHoverGroup;
} 